package com.sdry.service.impl.hpf;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.BeatMapper;
import com.sdry.mapper.hpf.PositionMapper;
import com.sdry.mapper.hpf.ReferenceMapper;
import com.sdry.mapper.hpf.TechnicsMapper;
import com.sdry.mapper.llm.EmployeeRecordMapper;
import com.sdry.model.hpf.Beat;
import com.sdry.model.hpf.Position;
import com.sdry.model.hpf.Reference;
import com.sdry.model.hpf.Technics;
import com.sdry.model.llm.EmployeeRecord;
import com.sdry.service.hpf.PositionService;

/**
 * @ClassName: PositionServiceImpl 
 * @Description:工位信息Service实现
 * @Author hpf
 * @Date 2018年9月21日 下午2:42:40 
 * @version 1.0
 */
@Service
public class PositionServiceImpl implements PositionService {

	@Resource
	private PositionMapper positionMapper;
	@Resource
	private TechnicsMapper technicsMapper;
	@Resource
	private BeatMapper beatMapper;
	@Resource
	private EmployeeRecordMapper employeeRecordMapper; // 注入员工记录mapper
	@Resource
	private ReferenceMapper referenceMapper; // 注入作业标准信息mapper
	
	/**
	 * 查询总数
	 * @param position
	 * @return
	 */
	public Integer countPosition(Position position) {
		return positionMapper.countPosition(position);
	}

	/**
	 * 分页查询
	 * @param map
	 * @return
	 */
	public List<Position> listPagePosition(Map<String, Object> map) {
		List<Position> list = positionMapper.listPagePosition(map);
		if(list.size() > 0){
			for(Position position : list){
				if(null != position.getTid() && !"".equals(position.getTid())){
					Technics technecs = technicsMapper.getTechnicsByTid(position.getTid());
					if(null != technecs){
						if(null != technecs.getTname() && !"".equals(technecs.getTname())){
							position.setRemark01(technecs.getTname());
						}
					}
				}
				if(null != position.getBid() && !"".equals(position.getBid())){
					Beat beat = beatMapper.getBeatByBid(position.getBid());
					if(null != beat){
						if(null != beat.getBtime() && !"".equals(beat.getBtime())){
							position.setRemark02(beat.getBtime().toString());
						}
					}
				}
			}
		}
		return list;
	}

	/**
	 * 新增工位信息
	 * @param position
	 * @return
	 */
	public Integer insertPosition(Position position) {
		int isSuccess = -2;
		int i = positionMapper.insertPosition(position);
		if(i > 0){
			isSuccess = 2;
		}
		return isSuccess;
	}

	/**
	 * 修改工位信息
	 * @param position
	 * @return
	 */
	public Integer updatePosition(Position position) {
		int isSuccess = -3;
		Position po = positionMapper.getPositionByPid(position.getPid());
		if(po.getPname().equals(position.getPname())){
			int i = positionMapper.updatePosition(position);
			if(i > 0){
				isSuccess = 3;
			}
		}else{
			int i = positionMapper.updatePosition(position);
			if(i > 0){
				isSuccess = 3;
			}
		}
		return isSuccess;
	}

	/**
	 * 校验是否删除工位
	 * @param pid
	 * @return
	 */
	public Integer deletePositionOfVerify(String str) {
		int isSuccess = -1;
		String[] split = str.split(",");
		for(int i = 0; i < split.length; i ++){
			Long id = Long.parseLong(split[i]);
			List<EmployeeRecord> erList = employeeRecordMapper.getEmployeeRecordByPid(id);
			List<Reference> referenceList = referenceMapper.getReferenceByPositionId(id);
			if(erList.size() > 0 || referenceList.size() > 0){
				isSuccess = 1;
				break;
			}
		}
		return isSuccess; 
	}
	
	/**
	 * 删除工位信息
	 * @param idArr
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer deletePosition(String str) {
		int isSuccess = 0;
		try {
			String[] idArr = str.split(",");
			for (int i = 0; i < idArr.length; i++) {
				isSuccess = positionMapper.deletePosition(Long.parseLong(idArr[i]));
			}
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}

	/**
	 * 查询所有工位信息
	 * @return
	 */
	@Override
	public List<Position> listAllPosition() {
		return positionMapper.listAllPosition();
	}

	/**       
	 * @param pname
	 * @return    
	 */
	@Override
	public int verifyPositionName(String pname) {
		int isSuccess = 0;
		List<Position> list = positionMapper.getPositionByPname2(pname);
		if(list.size() > 0){
			isSuccess = 1;
		}else{
			isSuccess = -1;
		}
		return isSuccess;
	}

}
